class Solution {
public:

    int check(int mid, vector<int>& weights)
    {
        int count = 1;
        int temp = 0;
        for (int i = 0; i < weights.size(); i++)
        {
            if (temp + weights[i] > mid)
            {
                temp = 0;
                count++;
            }
            temp += weights[i];
        }
        return count;
    }


    int shipWithinDays(vector<int>& weights, int days) {
        int left = *max_element(weights.begin(), weights.end());
        int right = accumulate(weights.begin(), weights.end(), 0);
        while (left < right)
        {
            int mid = (left + right) / 2;
            if (check(mid, weights) > days)
            {
                left = mid + 1;
            }
            else
            {
                right = mid;
            }
        }
        return left;
    }
};